import os
from abc import abstractmethod

from database.mapper import stock_monitor
from database.service import monitor_result_service
from service import bs_service
from service.bs_service import BsContext
from utils import parse_util, excel_util, read_config


class Strategy:
    def __init__(self, name):
        self.name = name

    # 执行策略
    @abstractmethod
    def execute(self):
        print('开始执行策略' + str(self.name))


# 验证大资金
def yz_comprehensive():
    print('验证大资金')
    uast = stock_monitor.select_all('tbX50i9')
    all_list = []
    for ums in uast:
        s_code = ums.get('code')
        bs_context = BsContext(s_code, ums.get('name'))
        task_c_list = monitor_result_service.query_task_c_by_code(s_code)
        strategy3(bs_context, task_c_list)
        all_list += bs_context.deal_info
    excel_util.gen_an_excel(os.path.join(read_config.data_path, 'result', 'h验证'), '监控h验证', '',
                            parse_util.list_dict_to_arr(all_list))


# 资金和均线组合3
# 条件：1.出现连续两个买入信号
#       2.跌破五日均线和十日均线或者主力资金卖出
def strategy3(bs_context, task_c_list):
    print('组合3')
    range_len = len(task_c_list)
    for i in range(range_len - 1):
        tci0 = task_c_list[i + 1]
        tci1 = task_c_list[i]
        if (tci0.bs == 1 and tci1.bs == 1) and \
                ((tci0.break_ave5 and tci1.break_ave5) or
                 (tci0.break_ave10 and tci1.break_ave10) or
                 (tci0.main_io_buy and tci1.main_io_buy)):
            bs_service.buy(bs_context, 100, tci0.rt_price, tci0.rq)
        elif tci0.bs == 2 and ((tci0.rt_price < tci0.ave10 and tci0.rt_price < tci0.ave5) or tci0.main_io_sell):
            bs_service.sell(bs_context, 0, tci0.rt_price, tci0.rq)
    if len(bs_context.deal_info) < 2:
        return
    for info in bs_context.deal_info:
        print(info)


# 资金和均线组合2
# 条件：1.出现连续两个买入信号
#       2.跌破五日均线和十日均线或者主力资金卖出
def strategy2(bs_context, task_c_list):
    range_len = len(task_c_list)
    for i in range(range_len - 1):
        tci0 = task_c_list[i + 1]
        tci1 = task_c_list[i]
        if (tci0.bs == 1 and tci1.bs == 1) and \
                ((tci0.break_ave5 and tci1.break_ave5) or
                 (tci0.break_ave10 and tci1.break_ave10) or
                 (tci0.main_io_buy and tci1.main_io_buy)):
            bs_service.buy(bs_context, 100, tci0.rt_price, tci0.rq)
        elif tci0.bs == 2 and ((tci0.rt_price < tci0.ave10 and tci0.rt_price < tci0.ave5) or tci0.main_io_sell):
            bs_service.sell(bs_context, 0, tci0.rt_price, tci0.rq)
    if len(bs_context.deal_info) < 2:
        return
    for info in bs_context.deal_info:
        print(info)


# 资金和均线组合1
def strategy1(bs_context, task_c_list):
    range_len = len(task_c_list)
    for i in range(range_len - 1):
        tci0 = task_c_list[i + 1]
        tci1 = task_c_list[i]
        if (tci0.bs == 1 and tci1.bs == 1) and \
                ((tci0.break_ave5 and tci1.break_ave5) or
                 (tci0.break_ave10 and tci1.break_ave10) or
                 (tci0.main_io_buy and tci1.main_io_buy)):
            bs_service.buy(bs_context, 100, tci0.rt_price, tci0.rq)
        elif tci0.bs == 2 and (tci0.below_ave10 or tci0.below_ave5 or tci0.main_io_sell):
            bs_service.sell(bs_context, 0, tci0.rt_price, tci0.rq)
    if len(bs_context.deal_info) < 2:
        return
    for info in bs_context.deal_info:
        print(info)


if __name__ == '__main__':
    yz_comprehensive()
